package com.eastfair.certificate.controller;

import com.eastfair.annotation.annotation.base.BaseControllerInfo;
import com.eastfair.certificate.dto.FormFieldDTO;
import com.eastfair.certificate.entity.FormField;
import com.eastfair.certificate.dto.FormFieldSaveDTO;
import com.eastfair.certificate.dto.FormFieldUpdateDTO;
import com.eastfair.certificate.dto.FormFieldPageQuery;
import com.eastfair.certificate.service.FormFieldService;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.eastfair.boot.controller.SuperController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.certificate.vo.FormFieldVO;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.echo.core.EchoService;
import com.eastfair.core.base.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.eastfair.annotation.annotation.security.PreAuth;

import com.eastfair.boot.request.PageParams;
import com.eastfair.core.context.ContextUtil;


/**
 * <p>
 * 前端控制器
 * 动态表单
 * </p>
 *
 * @author ligang
 * @date 2023-05-17
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/formField")
@Api(value = "FormField", tags = "动态表单")
@PreAuth(replace = "certificate:formField:")
public class FormFieldController extends SuperController<FormFieldService, Long, FormField, FormFieldPageQuery, FormFieldSaveDTO, FormFieldUpdateDTO> {

    @Autowired
    private EchoService echoService;

    @Override
    public void handlerResult(IPage<FormField> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }
    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<FormField> formFieldList = list.stream().map((map) -> {
            FormField formField = FormField.builder().build();
            //TODO 请在这里完成转换
            return formField;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(formFieldList));
    }



    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<FormFieldPageQuery> params) {
        FormFieldPageQuery pageQuery = params.getModel();

    }


    /**
     * 修改表单配置
     *
     * @param formFieldDTO 表单实体
     * @return 是否成功
     */
    @ApiOperation(value = "修改表单配置", notes = "修改表单配置")
    @PutMapping("/updateFormField")
    public R<Boolean> updateFormField(@RequestBody FormFieldDTO formFieldDTO) {
        return baseService.updateFormField(formFieldDTO);
    }

    /**
     * 删除表单配置
     *
     * @param id 主键
     * @return 是否成功
     */
    @ApiOperation(value = "删除表单配置", notes = "删除表单配置")
    @DeleteMapping("/{id}")
    public R<Boolean> deleteById(@PathVariable("id") Long id) {
        return R.success(baseService.updateById(FormField.builder().id(id).isDeleted(BusinessConstant.DELETE_YES).build()));
    }

    /**
     * 根据表单类型获取表单
     *
     * @param formFieldDTO 表单过滤条件
     * @return 表单列表
     */
    @ApiOperation(value = "根据表单类型获取表单", notes = "根据表单类型获取表单")
    @PostMapping("/list")
    @BaseControllerInfo()
    public R<List<FormFieldVO>> list(@RequestBody FormFieldDTO formFieldDTO) {
        return R.success(baseService.doToVos(baseService.list(formFieldDTO, null)));
    }


    /**
     * 置顶
     *
     * @param id 主键id
     * @return 表单列表
     */
    @ApiOperation(value = "置顶", notes = "置顶")
    @GetMapping("/top")
    public R top(Long id) {
        int num = 1;
        FormField formField = baseService.getByIdCache(id);
        if (formField == null) {
            return R.success();
        }
        formField.setSequence(num++);
        baseService.updateById(formField);
        //其他重新排序
        FormFieldDTO queryFormField = new FormFieldDTO();
        queryFormField.setGroupId(formField.getGroupId());
        List<FormField> formFieldList = baseService.list(queryFormField, null);
        if (formFieldList != null && !formFieldList.isEmpty()) {
            for (FormField field : formFieldList) {
                if (!field.getId().equals(formField.getId())) {
                    field.setSequence(num++);
                    baseService.updateById(field);
                }
            }
        }
        return R.success();
    }


    /**
     * 校验动态表单
     *
     * @param formFieldVOList
     * @return 是否成功
     */
    @ApiOperation(value = "校验动态表单", notes = "校验动态表单")
    @PostMapping("/verifyForm")
    public R verifyForm(@RequestBody List<FormFieldVO> formFieldVOList) {
        R<String> r = baseService.checkFormFieldInfo(formFieldVOList);
        if (r.getData().length() > 0) {
            return r;
        }
//        R<Test1DynamicForm> test1DynamicFormR = convertByFormField.assembleFieldToObject(formFieldParamList, Test1DynamicForm.class);
//        return test1DynamicFormR;
        return R.success();
    }

    /**
     * 查询提交后的表单填写信息
     *
     * @param map 表单组id
     * @return
     */
    @ApiOperation(value = "查询提交后的表单填写信息", notes = "查询提交后的表单填写信息")
    @PostMapping("/findsubmitForm")
    public R findsubmitForm(@RequestBody Map<String, Object> map) {
        Long groupId = Long.parseLong(map.get("groupId").toString());
        Object object = map.get("object");

//        FormField formField = new FormField();
//        formField.setGroupId(groupId);
//        formField.setIsEnabled(BusinessConstant.YES);
//        List<FormFieldVO> formFieldVOList = baseService.doToVos(baseService.list(formField, null));
//        R<List<FormFieldSubsystemVO>> r = convertByFormField.assembleObjectToField(formFieldVOList, object);
        return R.success();
    }
}
